package com.ljx.offer;

/**
 * @author 李捷禧
 * Date: 2023/3/25
 * ClassName: C_04
 */

public class C_04 {
    /**
     * 从左下角开始遍历
     * @param matrix
     * @param target
     * @return
     */
    public boolean findNumberIn2DArray1(int[][] matrix, int target) {
        int i = matrix.length - 1, j = 0;
        while (i >= 0 && j < matrix[0].length){
            if(matrix[i][j] < target) i--;
            else if(matrix[i][j] > target) j++;
            else return true;
        }
        return false;
    }

    /**
     * 二叉搜索树
     * @param matrix
     * @param target
     * @return
     */
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if(matrix[i][j] == target) return true;
            }
        }
        return false;
    }

}
